$(function () {


    $("#topBankRank").attr("class", "active");

    //加载表格
    initDataGrid();


    $('#queryDate').daterangepicker(
        {
            opens: 'left',
            format: 'yyyy-MM-dd',
            separator: '至',
            startDate: Date.today().add({days: -29}),
            endDate: Date.today(),
            locale: {
                fromLabel: 'From',
                toLabel: 'to',
                daysOfWeek: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
                monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
                firstDay: 1
            },
            showWeekNumbers: true,
        },
        function (start, end) {
            $('#queryDate span').html(start.toString('yyyy-MM-dd') + ' - ' + end.toString('yyyy-MM-dd'));
        }
    );

})


function initDataGrid() {
    //创建bootstrapTable
    $("#dataGrid").bootstrapTable('destroy').bootstrapTable({
        method: "GET",
        dataType: "json",
        url: '../admin/getTopBrandRankList',
        pagination: true,//显示分页条：页码，条数等
        striped: true,//隔行变色
        pageNumber: 1,//首页页码
        pageSize: 10,//分页，页面数据条数
        pageList: [5, 10, 15, 20],
        queryParamsType: "",
        queryParams: function (params) {
            var createDate = $("#queryDate").val();
            var startDate = createDate.substr(0, createDate.indexOf('至'));
            var endDate = createDate.substr(createDate.indexOf('至') + 1, createDate.length);

            var name = $("#queryName").val();
            var isRecommend = $("#isRecommend").val();
            var phone = $("#queryPhone").val();

            return {
                // 说明：传入后台的参数包括offset开始索引，limit步长，sort排序列，order：desc或者,以及所有列的键值
                pageSize: params.pageSize,
                pageNum: params.pageNumber,
                phone: phone,
                name: name,
                isRecommend: isRecommend,
                startTime: startDate,
                endTime: endDate
            };
        },
        uniqueId: "id",//Indicate an unique identifier for each row
        sidePagination: "server",//在服务器分页
        sortName: "id",
        toolbar: "#toolbar",//工具栏
        columns: [{
            checkbox: "true",
            field: "box"
        }, {
            title: "ID",
            field: "id",
            visible: false,
            sortable: true
        }, {
            title: "品牌LOGO",
            field: "logo",
            formatter: function (value, row, index) {
                if (value == "") {
                    return "无"
                } else {
                    return '<img  src=' + value + ' width="100" height="100" >';
                }
            }
        }, {
            title: "品牌名称",
            field: "name"
        }, {
            title: "手机号码",
            field: "phone"
        }, {
            title: "是否推荐",
            field: "isRecommend",
            formatter: function (value, row, index) {
                if (value) {
                    return "已推荐"
                } else {
                    return "未推荐"
                }
            }
        }, {
            title: "是否发布",
            field: "isShow",
            formatter: function (value, row, index) {
                if (value) {
                    return "发布"
                } else {
                    return "未发布"
                }
            }
        }, {
            title: "排序",
            field: "sort"
        }, {
            title: "上传时间",
            field: "createTime",
            formatter: function (value, row, index) {
                return changeDateFormat(value)
            }
        }, {
            title: "操作",
            width: "200px",
            formatter: function (value, row, index) {
                var id = row.id
                var sort = row.sort
                return '<input type="button" value="设置排序" data-toggle="modal" data-target="#sortModal"  class="btn btn-primary" onclick="editSort(' + id + ',' + sort + ')" />'
            }
        }]
    });
}

function query() {
    initDataGrid();
}


function editSort(id, sort) {
    $("#id").val(id)
    $("#sort").val(sort)
    $("#sortModal").modal("show");
}

$("#submit").click(function () {
    $.ajax({
        url: "../admin/editTopBrandSort",
        type: "post",
        data: {
            "id": $("#id").val(),
            "sort": $("#sort").val()
        },
        dataType: "json",
        async: false,
        success: function (data) {
            $("#sortModal").modal('hide')
            $("#dataGrid").bootstrapTable("refresh");
        },
        error: function (data) {
        }
    });
})




//转换日期格式(时间戳转换为datetime格式)
function changeDateFormat(cellval) {
    var dateVal = cellval + "";
    if (cellval != null) {
        var date = new Date(parseInt(dateVal.replace("/Date(", "").replace(")/", ""), 10));
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
        return date.getFullYear() + "-" + month + "-" + currentDate + " " + hours + ":" + minutes + ":" + seconds;
    }
}








